package com.kevin.testsensors;

import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.util.Log;
import android.app.Activity;
import android.content.Context;

public class MainActivity extends Activity implements SensorEventListener {
	private final static String TAG = "MainActivity";
	private SensorManager mSensorManager;
	private Sensor mAccelSensor;
	
	private float mLastXAccel = 0;
	private float mLastYAccel = 0;
	private float mLastZAccel = 0;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
		mAccelSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
	}

	@Override
	protected void onResume() {
		if (null != mAccelSensor) {
			mSensorManager.registerListener(this, mAccelSensor, SensorManager.SENSOR_DELAY_NORMAL);
		}
		
		super.onResume();
	}

	@Override
	protected void onPause() {
		if (null != mAccelSensor) {
			mSensorManager.unregisterListener(this);
		}
		
		super.onPause();
	}

	@Override
	public void onSensorChanged(SensorEvent event) {
		if (mLastXAccel - event.values[0] > 1 ||
			mLastYAccel - event.values[1] > 1 ||
			mLastZAccel - event.values[2] > 1) {
			Log.d(TAG, "x: " + event.values[0] + ", y: " + event.values[1] + ", z: " + event.values[2]);
		}

		mLastXAccel = event.values[0];
		mLastYAccel = event.values[1];
		mLastZAccel = event.values[2];
	}

	@Override
	public void onAccuracyChanged(Sensor sensor, int accuracy) {
		
	}

}
